<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/28
 * Time: 13:52
 */
namespace common\sources\read\taskcommod;

use common\models\craftsman\TaskCommod;

class TaskCommodAccess implements ITaskCommodAccess
{
    public function getTaskDetail($task_commod_id)
    {
        return TaskCommod::find()
            ->select("id,plat_type,shop_id,bus_id,shop_name,task_type,port_type,title,main_url1,main_url2,colour,size,price,num,task_answer,task_num,task_status,keywords,task_id,surplus_task_num,freight_deposit,
                prompt,com_amount,ship_addr,buy_get_commisse,commod_link,range_price,keywords,qr_code_url,return_type,shop_id,item_commisse,pay_task_commisse")
            ->where(["id"=> $task_commod_id])
            ->asArray()
            ->one();
    }

    public function getCount($search)
    {
        $where = $this->getWhere($search);
        $modes = TaskCommod::find()
                ->alias("t")
//                ->select("t.id,t.task_id,t.bus_phone,t.shop_name,t.plat_type,t.task_num,t.task_answer,t.task_type,t.task_status")
                ->leftJoin("value_service v",'v.task_commod_id = t.id')
                ->leftJoin("shop h" ,"h.id = t.shop_id")
                ->where($where)
                ->count();
        return $modes;
    }


    public function getList($search)
    {
        $where = $this->getWhere($search);
        $modes = TaskCommod::find()
            ->alias("t")
            ->select("t.id,t.task_id,t.bus_phone,t.shop_name,t.plat_type,t.task_num,t.task_answer,t.task_type,t.task_status,pay_time,v.prio_audit")
            ->leftJoin("value_service v",'v.task_commod_id = t.id')
            ->leftJoin("shop h" ,"h.id = t.shop_id")
            ->where($where)
            ->orderBy("t.create_time desc");
        if(isset($search['page_count']) && !empty($search['page_count']))
        {
            $modes->offset(($search['page_num'] -1)* $search['page_count'])
                    ->limit($search['page_count']);
        }
        return $modes->asArray()
                        ->all();
    }

    public function getDetial($task_commod_id)
    {
        return TaskCommod::find()
                ->alias("t")
                ->select("t.id,t.bus_id,t.plat_type,t.shop_id,t.task_type,t.port_type,t.price,t.range_price,t.ship_addr,t.num,t.is_task_image,t.task_id,
                t.task_answer,t.task_num,t.prompt,t.bus_phone,t.create_time,t.task_status,t.com_amount,t.margin,t.pay_task_amount,t.item_commisse,
                t.buy_get_commisse,t.com_task_commisse,t.pay_task_commisse,t.service_amount,t.pay_time,t.is_print,
                t.commod_link,t.title,t.main_url1,t.main_url2,t.colour,t.size,t.price,t.range_price,t.surplus_task_num,
                h.shop_name,h.ww_name,h.shop_type,h.home_url,
                u.qq,u.wx")
                ->leftJoin("shop h" ,"h.id = t.shop_id")
                ->leftJoin("buyuser u" ,"u.id = h.bus_id")
                ->where(["t.id" => $task_commod_id])
                ->asArray()
                ->one();
    }

    private function getWhere($search)
    {
        $where = ' 1=1  ';
        if(isset($search['plat_type']) && !empty($search['plat_type']))
        {
            $where .= " AND  t.plat_type = ".$search['plat_type'];
        }
        if(isset($search['task_status']) && !empty($search['task_status']))
        {
            $where .= " AND t.task_status = ". $search['task_status'];
        }
        if(isset($search['task_type']) && !empty($search['task_type']))
        {
            $where .= " AND t.task_type = ". $search['task_type'];
        }
        if(isset($search['prio_audit']) && !empty($search['prio_audit']))
        {
            $where .= " AND v.prio_audit = '1'";
        }
        if(isset($search['search_type']) && !empty($search['search_type']))
        {
            if($search['search_type'] == 1)
            {
                $where .= " AND t.task_id = ". "'" . $search['search_text'] . "'";
            }else if($search['search_type'] == 2){
                $where .= " AND t.bus_phone = "."'" . $search['search_text'] . "'";
            }else if($search['search_type'] == 3){
                $where .= " AND t.shop_name = ". "'" . $search['search_text'] . "'";
            }else if($search['search_type'] == 4){
                $where .= " AND h.ww_name = ". "'" . $search['search_text'] . "'";
            }
        }
        if((isset($search['task_start_time']) && !empty($search['task_start_time'])) && (isset($search['task_end_time']) && !empty($search['task_end_time'])))
        {
            $where .= " AND t.create_time>=" .$search['task_start_time']." AND t.create_time<=". $search['task_end_time'];
        }
        return $where;
    }

    public function checkTask($task_id) {
        $check = TaskCommod::find()
            ->select("bus_id")
            ->where(['task_id' => $task_id])
            ->asArray()
            ->one();
        return $check;
    }

    public function AuditDataRefus($data) {
        $query = TaskCommod::find()
            ->where(['task_status' => 3])
            ->andwhere(['audit_operator' => $data['worker']]);
        if (!empty($data['begin_time'])) {
            $query->andWhere([
               '>',
               'audit_time',
               $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        $num = $query->count();
        return $num;
    }

    public function AuditDataPass($data) {
        $query = TaskCommod::find()
            ->where(['task_status' => [4,5,6]])
            ->andwhere(['audit_operator' => $data['worker']]);
        if (!empty($data['begin_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query->andWhere([
                '>',
                'audit_time',
                $data['begin_time']
            ]);
        }
        $num = $query->count();
        return $num;
    }

    public function taskOrderBy($data) {
        if (1 == $data['orderTime']) {
            $time = strtotime('-1week');
        }
        if (2 == $data['orderTime']) {
            $time = strtotime('-1month');
        }
        $sql = "select count(bus_id) as num,bus_id from task_commod " . " where create_time >" . $time . " group by bus_id order by num desc LIMIT " . $data['limit'];
        $db = \Yii::$app->db->createCommand($sql)->queryAll();
        return $db;
    }


    //获取商铺任务
	public function getShopTaskCount($where) {
		return TaskCommod::find()->where($where)->count();
	}

	//任务详情
	public function getTaskCommodInfo($task_commod_id) {
		return TaskCommod::find()
			->select("id,pay_task_commisse,pay_task_amount,com_task_commisse,plat_type,task_type,shop_id,shop_name,port_type,commod_link,title,main_url1,main_url2,colour,size,price,num,range_price,ship_addr,qr_code_url,
                task_answer,task_num,keywords,task_status,margin,item_commisse,buy_get_commisse,service_amount,bus_id,create_time,public_task_status,task_id,is_task_image,img_praise_type,img_prase_type_pepper,freight_deposit,surplus_task_num,
                task_img_pepper,prompt,com_amount,freight_deposit,total_amount,item_task_amount")
			->where(['id' => $task_commod_id])
			->asArray()
			->one();
	}


	/**
	 * getTaskDetial 根据主任务编号获取住任务详情
	 * @param $task_id
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/21
	 * @return array|TaskCommod|\yii\db\ActiveRecord|null
	 */
	public function getTaskDetial($task_id) {
		$check = TaskCommod::find()
			->select("id,shop_id,bus_id,bus_phone,task_id,pay_task_commisse,task_status,com_task_commisse,buy_get_commisse,freight_deposit,pay_task_amount,margin,com_amount,item_commisse,task_num,surplus_task_num")
			->where(['task_id' => $task_id])
			->asArray()
			->one();
		return $check;
	}
	//获取未审核任务数量
    public function getTaskCommodAuditCount($data)
    {
        return TaskCommod::find()
                ->where($data)
                ->count();
    }

}